# -*- coding: utf-8 -*-
#'django-serializable'
#Copyright (C) 2010 Sébastien Piquemal @ futurice
#contact : sebastien.piquemal@futurice.com
#futurice's website : www.futurice.com

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
..
    >>> import pickle
    >>> import json
    >>> from django.test.client import Client
    >>> from .models import *

    >>> a = Author(firstname="John", lastname="Steinbeck") ; a.save()
    >>> b = Book(title="Grapes of Wrath", author=a, comments="great great great") ; b.save()
    >>> b2 = Book(title="East of Eden", author=a, comments="great great great") ; b2.save()
    
    >>> c = Client()

Getting a serializable
########################

    >>> get_url = '/author/%s/%s/' % (a.firstname, a.lastname)
    >>> response = c.get(get_url).content #'c' is a HTTP client,
    >>> pickle.loads(response) == a.serialize() # 'response' is a pickled dictionnary
    True

Getting a list of serializable
###################################

With a filter
--------------

    >>> get_url = '/book/list/%s/' % "Steinbeck"
    >>> response = c.get(get_url).content
    >>> pickle.loads(response) == [bl.serialize() for bl in Book.objects.filter(author__lastname="Steinbeck")]
    True
    >>> len(pickle.loads(response))
    2

Without filter
-----------------

    >>> get_url = '/book/list/'
    >>> response = c.get(get_url).content
    >>> pickle.loads(response) == [bl.serialize() for bl in Book.objects.all()]
    True
    >>> len(pickle.loads(response))
    2

Creating a serializable 
#################################

    >>> posted_data = {"serializable_data": pickle.dumps({}) } #data sent with the POST
    >>> post_url = '/author/%s/%s/' % ("Emile", "Zola")
    >>> response = c.post(post_url, data=posted_data).content
    >>> a2 = Author.objects.get(firstname="Emile", lastname="Zola")
    >>> pickle.loads(response) == a2.serialize()
    True

Updating a serializable 
#################################

    >>> posted_data = {"serializable_data": pickle.dumps({"author": ("Emile", "Zola"), "title": "Germinal"}) }#data sent with the post
    >>> post_url = '/book/%s/' % b.pk
    >>> response = c.post(post_url, data=posted_data).content
    >>> b = Book.objects.get(pk=b.pk)
    >>> pickle.loads(response) == b.serialize()
    True
    >>> b.title
    u'Germinal'

Deleting a serializable 
#################################

    >>> post_url = "/book/delete/%s/" % b.pk
    >>> response = c.post(post_url, data=posted_data).content
    >>> try :
    ...     Book.objects.get(pk=b.pk)
    ... except Book.DoesNotExist :
    ...     print "book was deleted"
    book was deleted

..

    json 
    ######
    >>> def tuple_values_to_lists(a_dict): #because 'json' takes tuple to lists.
    ...     returned = dict()
    ...     for key, value in a_dict.iteritems() :
    ...         returned[key] = list(value) if isinstance(value, tuple) else value
    ...     return returned  

    Getting
    ----------
    >>> get_url = '/book-json/%s/' % b2.pk
    >>> response = c.get(get_url).content
    >>> json.loads(response) == {u'author': [u'John', u'Steinbeck'], u'title': u'East of Eden'}
    True

    Creating
    ---------
    >>> posted_data = {"serializable_data": json.dumps({"comments": "super", "title": "Of mice and men", "author": ("John", "Steinbeck")}) } #data sent with the POST
    >>> post_url = '/book-json/%s/' % 888
    >>> response = c.post(post_url, data=posted_data).content
    >>> b3 = Book.objects.get(pk=888)
    >>> tuple_values_to_lists(b3.serialize()) == json.loads(response)
    True

    List
    -----------
    >>> get_url = '/book-json/list/'
    >>> response = c.get(get_url).content
    >>> json.loads(response) == [tuple_values_to_lists(bl.serialize()) for bl in Book.objects.all()]
    True
    >>> len(json.loads(response))
    2

    cleaning-up
    -------------
    >>> a.delete() ; a2.delete()
    >>> b2.delete() ; b3.delete()
"""
